/**
 * Copyright 2011 Enjoy Change, Inc. All rights reserved.
 * Use is subject to license terms.
 */
package com.enjoychange.enjoy.application.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.enjoychange.common.tool.MD5Helper;
import com.enjoychange.enjoy.application.IAccountService;
import com.enjoychange.enjoy.domain.Account;
import com.enjoychange.enjoy.domain.specification.AccountSpecification;
import com.enjoychange.enjoy.exception.AccountException;
import com.enjoychange.enjoy.infrastructure.IAccountRepository;

/**
 * @author Administrator
 * @date   May 31, 2011
 */
@Service("accountService")
public class AccountService implements IAccountService {
	
	
	@Autowired
	IAccountRepository accountRepository;
 
	/* (non-Javadoc)
	 * @see com.enjoychange.enjoy.application.IAccountService#createUser(com.enjoychange.enjoy.domain.User)
	 */
	@Override
	public void createAccount(Account account) throws AccountException {
		
		boolean isExist=AccountSpecification.isAccountExist(accountRepository, account);
		if(isExist){
			throw new AccountException("account_is_exist");
		}
		accountRepository.saveAccount(account);
	}

	@Override
	public void login(Account account) throws AccountException {
		String dbpassword=accountRepository.queryPasswordByEmail(account);
		boolean isvalid=AccountSpecification.isValidateAccount(account.getPassword(),dbpassword);
		if(isvalid){
			throw new AccountException("password_or_user_invaild");
		}
	}

}
